﻿
using Ldt.DataEngine.RuleAssemble;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.AnalysisModule.CaseCauseBaseInfoExtr
{
    /// <summary>
    /// 刑事案由提取
    /// </summary>
    public class ACaseCauseExt
    {
        /// <summary>
        /// 刑事案由的集合
        /// Cases the cause extract.
        /// </summary>
        /// <param name="caseCause">The case cause.</param>
        /// <param name="OriginalText">原文</param>
        /// <param name="caseType">案件类型</param>
        /// <returns>IList&lt;MSYS_CaseCause&gt;.</returns>
        public IList<CaseCauseModel> CaseCauseExtract(string caseCause, string OriginalText,string caseType)
        {
            IList<Tuple<string, string>> crimeNameList = new List<Tuple<string, string>>();
            IList<CaseCauseModel> reList = new List<CaseCauseModel>();
            GetSysCaseCause dalCause = new GetSysCaseCause();

            string MacthKeyWord = string.Empty;
            if (string.IsNullOrEmpty(caseCause))
            {   
                //如果案由为空
                var CaseCauseOriginal = " CS_CaseCauseRelation$OriginalText$XSCourtRecord";
                var OriginalrowList = RuleParseClass.GetParseList(CaseCauseOriginal, OriginalText);
                //提取到多个案由
                var CauseNameCode = " CS_CaseCauseRelation$CH_CaseCauseName$XSCourtRecord";
                string OriginalTexttemp = OriginalrowList.Count > 0 ? OriginalrowList[0] : OriginalText;
                var rowList = RuleParseClass.GetParseList(CauseNameCode, OriginalTexttemp);
                foreach (string cuseName in rowList)
                {
                    if (!crimeNameList.Any(tup => tup.Item1 == cuseName))
                    {
                        crimeNameList.Add(new Tuple<string, string>(cuseName, OriginalTexttemp));
                    }
                }
            }
            else
            {
                crimeNameList.Add(new Tuple<string, string>(caseCause, "平台传入的案由，不进行案由提取！Common"));
            }

            foreach (Tuple<string, string> causeName in crimeNameList)
            {
                ///0001.根据案由集合，进行根据案由名称匹配
                CaseCauseModel MCause = dalCause.GetModelByCauseName(causeName.Item1, caseType);
                if (MCause == null)
                {
                    /// 0002.根据案由集合，进行根据案由的ID匹配
                    MCause = dalCause.GetModel(causeName.Item1);
                }
                if (MCause == null)
                {
                    /// 0003.根据案由集合，进行根据案由名称模糊匹配
                    string causeID = dalCause.GetCauseID(causeName.Item1, "a");
                    MCause = dalCause.GetModel(causeID);
                }
                if (MCause != null)
                {
                    MCause.MatchOrginalText = causeName.Item2;
                    reList.Add(MCause);
                }
            }
            return reList;
        }
    }
}
